define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'onlineclassroom/onlineclassroomuser/index' + location.search,
                    approve_url: 'onlineclassroom/onlineclassroomuser/approve',
                    detail_url: 'onlineclassroom/onlineclassroomuser/detail',
                    del_url: 'onlineclassroom/onlineclassroomuser/del',
                    multi_url: 'onlineclassroom/onlineclassroomuser/multi',
                    import_url: 'onlineclassroom/onlineclassroomuser/import',
                    table: 'online_classroom_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        { field: "city_id", title: "城市" },
                        { field: "school_id", title:"学校"},
                        {field: 'user.username', title: '用户名', operate: 'LIKE'},
                        {field: 'user.nickname', title: '昵称', operate: 'LIKE'},
                        {field: 'user.mobile', title: '手机号', operate: 'LIKE'},
                        {field: 'classroom_id', title: '课堂ID'},
                        {field: 'join_time_text', title: '申请时间', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: '状态', searchList: {"0": __("待审核"), "1": __("已通过"), "2": __("已拒绝"), "3": __("已取消")}, formatter: Table.api.formatter.status},
                        {field: 'apply_reason', title: '申请理由', align: 'left', operate: 'LIKE', formatter: function(value, row, index) {
                            return value ? (value.length > 20 ? value.substr(0, 20) + '...' : value) : '';
                        }},
                        {field: 'approve_time_text', title: '审核时间', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', align: 'left', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            buttons: [
                                {
                                    name: 'detail',
                                    title: '详情',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'onlineclassroom/onlineclassroomuser/detail',
                                    extend: 'data-area=["95%","95%"] data-title="查看详情"'
                                },
                                {
                                    name: 'approve',
                                    title: '审核',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'onlineclassroom/onlineclassroomuser/approve',
                                    visible: function(row) {
                                        return row.status == 0; // 只有待审核状态才显示审核按钮
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            
            // 引入通用的城市学校过滤JS文件
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js';
            document.head.appendChild(script);

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 审核按钮事件
            $(document).on('click', '.btn-approve', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error(__('Please select at least one record'));
                    return false;
                }
                if (ids.length > 1) {
                    Toastr.error('请选择一条记录进行审核');
                    return false;
                }
                var url = 'onlineclassroom/onlineclassroomuser/approve/ids/' + ids.join(',');
                Fast.api.open(url, '审核申请', {
                    area: ['95%', '95%']
                });
            });

            // 批量审核按钮事件
            $(document).on('click', '.btn-batch-approve', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error(__('Please select at least one record'));
                    return false;
                }
                
                Layer.confirm('请选择批量审核操作', {
                    btn: ['批量通过', '批量拒绝', '取消']
                }, function(index) {
                    // 批量通过
                    Layer.close(index);
                    $.post('onlineclassroom/onlineclassroomuser/batchapprove', {
                        ids: ids.join(','),
                        status: 1
                    }, function(data) {
                        if (data.code === 1) {
                            Toastr.success(data.msg);
                            table.bootstrapTable('refresh');
                        } else {
                            Toastr.error(data.msg);
                        }
                    });
                }, function(index) {
                    // 批量拒绝
                    Layer.close(index);
                    Layer.prompt({
                        title: '请输入拒绝理由',
                        formType: 2
                    }, function(text, index2) {
                        if (!text) {
                            Toastr.error('请输入拒绝理由');
                            return false;
                        }
                        Layer.close(index2);
                        $.post('onlineclassroom/onlineclassroomuser/batchapprove', {
                            ids: ids.join(','),
                            status: 2,
                            reject_reason: text
                        }, function(data) {
                            if (data.code === 1) {
                                Toastr.success(data.msg);
                                table.bootstrapTable('refresh');
                            } else {
                                Toastr.error(data.msg);
                            }
                        });
                    });
                });
            });
        },
        
        approve: function () {
            Controller.api.bindevent();
        },
        
        detail: function () {
            // 详情页面不需要特殊处理
        },
        
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    
    return Controller;
});